package neusoft.yuejian_11.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.yuejian_11.adapter.McourseListAdapter;
import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.MCourseBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.MasterModel;
import neusoft.yuejian_11.R;

public class McourseListActivity extends BaseActivity implements RetrofitListener{
    protected Context context;
    private int masterId;
    private String masterName;
    private String masterPic;
    private TextView textViewMasterName;
    private ImageView imageViewMaster;
    private McourseListAdapter adapter;
    private RecyclerView recyclerView;
    private ImageButton imageButtonBack;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mcourse_list);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        textViewMasterName = (TextView) findViewById(R.id.textView_master_name);
        imageViewMaster = (ImageView) findViewById(R.id.imageViewMaster);

        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    }

    private void initEvents() {
        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    private void showData() {
        masterId = getIntent().getIntExtra("pm_id",-1);
        masterName = getIntent().getStringExtra("pm_name");
        textViewMasterName.setText(masterName);
        masterPic = getIntent().getStringExtra("pm_pic");
        Picasso.with(context)
                .load(masterPic)
                .placeholder(R.drawable.ic_club)
                .error(R.drawable.ic_noclub)
                .into(imageViewMaster);
    }

    private void initData() {
        MasterModel masterModel = new MasterModel();
        masterModel.mcourseList(String.valueOf(masterId),this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        List<MCourseBean> mCourseBeanList = (List<MCourseBean>) o;
        adapter = new McourseListAdapter(McourseListActivity.this,mCourseBeanList);
        LinearLayoutManager layoutManager = new LinearLayoutManager(McourseListActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onFail() {

    }
}
